package com.dhcc.sds.base.controller;


import java.util.List;
import java.util.Map;


import com.dhcc.core.framework.result.node.ZTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sds.base.entity.SdsDictypeEntity;
import com.dhcc.sds.base.service.ISdsDictypeService;


/**
 * 基础字典类型
 * 
 * @ClassName: SdsDictypeController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:10:52
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsdictype")
public class SdsDictypeController extends BaseController {

	private String PREFIX = "/backend/sds/sdsdictype/";

	@Autowired
	private ISdsDictypeService sdsDictypeService;
	

    /**
     * 跳转到基础字典类型列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "sdsdictype.html";
    }
	
    /**
     * 跳转到新增基础字典类型
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsdictype_add.html";
    }
	
	/**
     * 跳转到修改基础字典类型
     */
    @RequestMapping("/to_update/{sdsDictypeId}")
    public String toUpdate(@PathVariable Long sdsDictypeId, Model model) {
        SdsDictypeEntity sdsDictype = this.sdsDictypeService.selectById(sdsDictypeId);
        model.addAttribute("sdsDictype",sdsDictype);
        return PREFIX + "sdsdictype_edit.html";
    }

    /**
     * 获取基础字典类型列表-分页
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/page")
    @ResponseBody
    public Object page(@RequestParam Map<String, Object> params) {
		Page<SdsDictypeEntity> page = new PageFactory<SdsDictypeEntity>().defaultPage();
        List<SdsDictypeEntity> result = this.sdsDictypeService.page(page,params);
        page.setRecords((List<SdsDictypeEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 获取基础字典类型列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        List<SdsDictypeEntity> result = this.sdsDictypeService.list(params);
        return result;
    }

    /**
     * 获取基础字典类型列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() { return this.sdsDictypeService.tree(); }

    /**
     * 新增基础字典类型
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsDictypeEntity sdsDictype) {
        if (CommonUtil.isOneEmpty(sdsDictype)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsDictype.insert();
        return R.ok();
    }

    /**
     * 删除基础字典类型
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsDictypeId) {
	
        this.sdsDictypeService.deleteById(sdsDictypeId);

        return R.ok();
    }

    /**
     * 修改基础字典类型
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsDictypeEntity sdsDictype) {
        if (CommonUtil.isOneEmpty(sdsDictype)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsDictypeEntity old = this.sdsDictypeService.selectById(sdsDictype.getId());

        old.setTypeCode(sdsDictype.getTypeCode());
        old.setTypeDesc(sdsDictype.getTypeDesc());
        old.setIsActive(sdsDictype.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 基础字典类型置为无效
     */
    @RequestMapping(value = "/cancel")
    @ResponseBody
    public Object cancel(@RequestParam Long sdsDictypeId) {
        SdsDictypeEntity entity = new SdsDictypeEntity();
        entity.setId(sdsDictypeId);
        entity.setIsActive(0);
        this.sdsDictypeService.updateById(entity);

        return R.ok();
    }
	
}
